package com.amado.course34Thread;

/**
 * Created by Administrator on 2018/7/17.
 */
public class S10Wait {
	public static void main(String[] args) {
		// 对象
		Object lock = new Object();
		for (int i = 0; i < 2; i++) {

			new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						// 去先等待
						System.out.println("threadOne 我在等待");
						synchronized (lock) {
							// 等待200毫秒后自己唤醒
							lock.wait(200);
							// 一下等待
//							lock.wait();
						}
						System.out.println("threadOne 被唤醒");
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}).start();
		}
		new Thread(new Runnable() {
			@Override
			public void run() {
				for (int i = 0; i < 3; i++) {
					try {
						Thread.sleep(400);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					if (i == 2) {
						System.out.println("唤醒threadOne");
						synchronized (lock) {
							// 唤醒在此对象监视器上等待的单个线程（选择是任意性的，也就是随机）
//							lock.notify();
							// 唤醒在此对象监视器上等待的所有线程
							lock.notifyAll();
						}
					}
					System.out.println("threadTwo i=" + i);
				}
			}
		}).start();
	}
}
